﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace Sc.Web.Controllers.WebApi
{
    public class ToDoListController : ApiController
    {
        private readonly Sys_ToDoListService service = new Sys_ToDoListService();
        /// <summary>
        /// 获取当前用户待办事宜
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        [HttpPost]
        public object GetPageData()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            int pageSize = Convert.ToInt32(HttpContext.Current.Request["pageSize"]);
            int pageIndex = Convert.ToInt32(HttpContext.Current.Request["pageIndex"]);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var tmp = service.GetList(id, pageSize, pageIndex, 0);
            var data = json.Serialize(tmp);
            if (tmp == null)
            {
                data = "[]";
            }
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 获取当前用户已办事宜
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        [HttpPost]
        public object GetHasPageData()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            int pageSize = Convert.ToInt32(HttpContext.Current.Request["pageSize"]);
            int pageIndex = Convert.ToInt32(HttpContext.Current.Request["pageIndex"]);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var tmp = service.GetList(id, pageSize, pageIndex, 1);
            var data = json.Serialize(tmp);
            if (tmp == null)
            {
                data = "[]";
            }
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 获取未读消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetNoReadMsgCountByUser()
        {
            int userId = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            var mp = service.GetNoReadMsgCount(userId);
            return new HttpResponseMessage { Content = new StringContent(mp.ToString(), System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 更新待办事宜
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Update()
        {
            int tid = Convert.ToInt32(HttpContext.Current.Request["tid"]);
            Sys_ToDoList todo = new Sys_ToDoList() { Id = tid, State = 1 };
            var tmp = service.Update(todo) ? 1 : 0;
            return new HttpResponseMessage { Content = new StringContent(tmp.ToString(), System.Text.Encoding.UTF8, "application/json") };
        }
        [HttpPost]
        public void test()
        {
            CommPushMessage sm = new CommPushMessage();
            var token = HttpContext.Current.Request["token"];
            MessageParam m = new MessageParam();
            m.Content="ces";
            m.Perfix = "d";
            m.Title = "sds";
            m.UserIosToken = new List<string>() { "10EB55CBD1FDD3DD2CFE15D50965920803DCCE875477B74B8B52283944F51AEE" };
            sm.PushMessageForIOS(m);
        }
    }
}
